import HashMap from '@ohos.util.HashMap'
import { DuplicateNameError, DuplicateTypeError } from './Constant'

const TypeMap: HashMap<string, number> = new HashMap()

export function ModelType(feedType: number): Function {
  return (target: Function) => registerModel(target, feedType)
}

export function registerModel(target: Function, type: number): void {
  if (TypeMap.hasKey(target.name)) {
    throw new Error(`${target.name} ${DuplicateNameError}`)
  }
  if (TypeMap.hasValue(type)) {
    throw new Error(`${type} ${DuplicateTypeError}`)
  }
  TypeMap.set(target.name, type)
}

export function containsModelType(type: number): boolean {
  return TypeMap.hasValue(type)
}

export function isTargetModel(type: number, model: Function): boolean {
  return TypeMap.get(model.name) == type
}